<template>
  <lay-container fluid="true" style="padding: 10px">

    <lay-card>
          <lay-form style="margin-top: 20px">
            <lay-row>
              <lay-col :md="6">
                <lay-form-item label="关键字：" label-width="60">
                  <lay-input
                    v-model="qm.key" style="width: 90%" placeholder=" 文件名称"
                    :allow-clear="true"
                  ></lay-input>
                </lay-form-item>
              </lay-col>
              <lay-col :md="10">
                <lay-row>
                  <lay-col :md="12">
                    <lay-form-item label="上传日期：" label-width="70">
                      <lay-date-picker v-model="qm.dtb" placeholder="开始日期" allowClear simple ></lay-date-picker>                   
                    </lay-form-item>                    
                  </lay-col>
                  <lay-col :md="12">
                    <lay-form-item label="~" label-width="10">
                      <lay-date-picker v-model="qm.dte" placeholder="截至日期" allowClear simple></lay-date-picker>                   
                    </lay-form-item>      
                  </lay-col>
                </lay-row>
                 
              </lay-col>
              

              <lay-col :md="6">
                <lay-form-item label-width="0">
                  <lay-button type="primary" @click="toSearch">查询</lay-button>
                  <lay-button @click="toReset">重置</lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-card>

    <div class="maintable-box"  >
      <lay-table
      :height="'100%'"
            :page="page"
            :columns="columns"
            :dataSource="dataSource"
            :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys"
            even
            @row="rowClick"
            @Change="pageChange"
            @sortChange="sortChange"
           
          >
          <template v-slot:cover="{ data }">
            <img :src="data.coverUrl"  style="width:100px;height:100px;cursor: pointer;margin: 20px;"  @click="openIframe(data)">
          </template>
            <template v-slot:toolbar>
              <lay-row>
                <lay-col :md="18">
                  <lay-button size="sm" type="primary"
                              @click="addRow"
                              v-permission="['/htfile/index:auth,/htfile/up']"
                  >添加</lay-button>
                  <!--<lay-button size="sm">删除</lay-button> -->
                </lay-col>
                <lay-col style="text-align:right;">
                   
                   
                  <lay-button prefix-icon="layui-icon-export" suffix-icon=""
                    @click="tableExport"
                    >导出</lay-button>

                </lay-col>
              </lay-row>
              
            </template>

            <template v-slot:operator="{data }">
              <a :href="data.url" target="_blank" class="myhref">下载</a>

            </template>
           
          </lay-table>
    </div>

     
  </lay-container>
</template>

<script lang="ts">
import { ref, watch,reactive,onMounted,h } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";  
import MyWin from "../../library/MyWin"; 
import MyPage from "../../library/MyPage"; 
import MyAuth from "../../library/MyAuth"; 
import {MyExcel} from "../../library/MyExcel"; 
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport"; 
import add from "./add.vue";
import iframeview from "../htty/iframeview.vue";
export default {
  setup( ) {
    const baseUrl="/htfile";
    const baseQUrl=baseUrl+"/page";
    const pageName="文件";
 
    const myWin=ref<any>(null);
    const selectedKeys = ref(['']) 
    const defaultToolbar = ref(false)
    const page = ref(  new MyPage()  ); 
     
    const columns = [
      // {
      //   title: '复选',
      //   width: '50px',
      //   type: 'checkbox',
      //   fixed: 'left'
      // },
      {
        title: '序号',
        width: '50px' ,
        key: 'rowNo'
      },
      { key:'name',title: '图片', width: '120px', customSlot: 'cover',
      },

      { key:'name',title: '原文件名', width: '100px', },
      { key:'contentType',title: '类型', width: '100px', },
      { key:'crtUname',title: '创建人', width: '100px', },
      { key:'crtDt',title: '创建日期', width: '100px', },
      { key:'size',title: '大小', width: '100px', },
      { key:'url',title: '路径', width: '100px', },


      {
        title: '操作',
        width: '150px',
        customSlot: 'operator',
        // key: 'operator',
        fixed: 'right'
      }
    ]

    const dataSource = ref([]);

    const rowClick = function (data: any) {}

    const rowDoubleClick = function (data: any) {}

    const pageChange = function ({ current, limit }: any) {
      if(qm.pageSize!=limit)
      {
        qm.pageSize=limit ;
        qm.page=1;
        query(1); 
      }else{
        query(current)
      }
      

    }
    function toSearch() { 
      query(1);
    }
     
    function toReset() {
      qm.key= '';
      qm.dtb='';
      qm.dte='';       
    }

    const qm=reactive({
      key:"",
      dtb:"",
      dte:"", 
      page: 1,
      pageSize: 10,
      sortFields:"edtDt desc"
    }); 
    const query=async (pageIndex:number) =>{
      if(pageIndex){
        page.value.current=pageIndex;
        qm.page=pageIndex;
      }
      var r=await new MyHttp().page(baseQUrl,qm,
        { 
        },
        columns
      ); 
      dataSource.value=r.data.records;
      r.data.records.forEach( (element: any)  => {
        element.coverUrl=element.url+"&cover=1"
        element.showUrl=element.url+"&show=1"
      });
      page.value.total=r.data.totalRow;
    }
    const RefreshToDB=async()=>{
      var r=await new MyHttp().post(baseUrl+"/refreshtodb",qm,
        { 
          showProcessshade:true
        }
      ); 
      query(1);
    }
    const sortChange = (key: any, sort: number) => {
      qm.sortFields=`${key} ${sort}`;
      query(0);
    }
    const tableExport=async()=>{
     
      var cols=columns as Array<TableColumn>;
      new MySimpleExport().exportExcel(
        {
          qm,
          url:baseQUrl,
          name:pageName,
          columns:cols,
          lx:"page"
        }
        );
    }
    


    onMounted(() =>{  
      myWin.value=new MyWin();
      query(1);
    });


    const addRow=async()=>{

      var opt={
        title: '新增'+pageName,
        content: h( add,
            {
              onOk:(data:any)=>{
                query(0);
              }
            }
        ),
        area: ['300px','200px']
      };
      myWin.value.open(opt );
    }
    const openIframe=(data:any)=>{
      var opt={
        title: '预览 '+data.name,
        content: h( iframeview,
            {
               url:data.showUrl,
            }
        ),
        area: ['1200px','900px']
      };
      myWin.value.open(opt );

    }

    return {
      columns,
      dataSource,
      selectedKeys, 
      defaultToolbar,
      page,
      rowClick,
      rowDoubleClick,
      pageChange,
      toReset,
      toSearch,
       

      qm,
      query,
      RefreshToDB,
      sortChange,
      tableExport,
    
      myWin,

      addRow,
      openIframe,

    }
  }
}
</script>
